package com.xinqi.modules.course.demonstrate.service.impl;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.modules.course.course.mapper.CourseMapper;
import com.xinqi.modules.course.demonstrate.service.DemoDiscussService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

/**
 * 示范课程 -- 讨论
 */

@Service("demoDiscussService")
@RequiredArgsConstructor
public class DemoDiscussServiceImpl implements DemoDiscussService {
    private final IdentifierGenerator identifierGenerator;

    private final CourseMapper courseMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void initDiscussSql(Long userId, Long classId, Map<String, String> idsMap, Map<String, String> chapterMap) {
        StringBuilder sql = new StringBuilder();
        // 公告记录
        String noticeSql = "INSERT INTO discuss.co_course_discuss(`id`, `title`, `content`, `fileurls`, `videos`, `images`, `class_id`, `is_replay`, `comment_view`, `synchro`, `class_ids`, `is_top`, `top_time`, `is_super`, `now_release`, `timing`, `release_time`, `end_time`, `status`, `extra`, `chapter_id`, `is_teacher`, `is_delete`, `delete_time`, `create_by`, `create_time`, `update_by`, `update_time`) " +
            "VALUES (" + idsMap.get("1662785767952781314") + ", '关于保护环境的讨论', '生态环境是立身之本，请同学们谈谈如何保护环境。', NULL, NULL, NULL, 1661184856792928257, 1, 0, 0, NULL, 0, NULL, 0, 1, 0, '2023-05-28 19:39:55', '2023-05-28 21:42:00', 1, NULL, NULL, 1, 0, NULL, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 21:41:49')," +
            "(" + idsMap.get("1663424373238587393") + ", '《水的运动》—章后问题研究讨论 （必须回复）', '通过前面的学习，我们知道因为洋流、纬度、海陆等原因造成不同的气候，同时造成不同的地理环境。南极洲面积为1400万平方千米，95%以上的地方常年被冰雪覆盖，形成了巨大而厚实的冰盖。冰盖平均厚度达2450 米，冰雪总量约2700万立方千米，占全球冰雪总量的 90%以上，占全世界可用淡水的 72%。有人估算，这里的淡水资源可供全球使用7 500年。因此，南极洲是人类最大的淡水资源库，而且很少受到污染，水质极好。那么，我们能否利用南极的冰山解决沙特阿拉伯的缺水问题呢?请同学们通过讨论的方式探究这一课题，可通过图片或文字的形式将搜集到的资料和观点发表在评论区。', '[{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133476413-资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"name\\\":\\\"资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"type\\\":1,\\\"filename\\\":\\\"资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133476413-资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133476413-资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"size\\\":10621,\\\"ossFileName\\\":\\\"1685426133476413-资料 1 联合国教科文组织国际淡水年特使谈沙特阿拉伯的淡水资源.docx\\\",\\\"progress\\\":100},{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133477749-资料 2 南极冰山的形成.docx\\\",\\\"name\\\":\\\"资料 2 南极冰山的形成.docx\\\",\\\"type\\\":1,\\\"filename\\\":\\\"资料 2 南极冰山的形成.docx\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133477749-资料 2 南极冰山的形成.docx\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426133477749-资料 2 南极冰山的形成.docx\\\",\\\"size\\\":10293,\\\"ossFileName\\\":\\\"1685426133477749-资料 2 南极冰山的形成.docx\\\",\\\"progress\\\":100}]', NULL, '[{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436933999-u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"name\\\":\\\"u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"type\\\":2,\\\"filename\\\":\\\"u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436933999-u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436933999-u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"size\\\":152073,\\\"ossFileName\\\":\\\"1685426436933999-u=3356859637,398958181&fm=253&fmt=auto&app=120&f=JPEG.webp.jpg\\\",\\\"width\\\":640,\\\"height\\\":384,\\\"progress\\\":100},{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436931639-fpsdr441maqfpsdr441maq.jpg\\\",\\\"name\\\":\\\"fpsdr441maqfpsdr441maq.jpg\\\",\\\"type\\\":2,\\\"filename\\\":\\\"fpsdr441maqfpsdr441maq.jpg\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436931639-fpsdr441maqfpsdr441maq.jpg\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685426436931639-fpsdr441maqfpsdr441maq.jpg\\\",\\\"size\\\":581159,\\\"ossFileName\\\":\\\"1685426436931639-fpsdr441maqfpsdr441maq.jpg\\\",\\\"width\\\":1920,\\\"height\\\":1080,\\\"progress\\\":100}]', 1661184856792928257, 1, 0, 0, NULL, 0, NULL, 0, 0, 0, '2023-05-30 20:12:23', '2023-06-20 00:00:00', 1, NULL, 1661193644786749441, 1, 0, NULL, 1645325062446964737, '2023-05-30 13:57:30', 1645325062446964737, '2023-05-31 11:47:55')," +
            "(" + idsMap.get("1663429659873566721") + ", '评述植物生长调节剂在生产中的应用', '查看下列资料，在评论区回复以下问题。\\n1.你还知道哪些植物生长调节剂在农产品和园艺生产上应用的例子?\\n2.在生产过程中施用植物生长调节剂要注意哪些事项?\\n3.我国禁止销售、使用未经国家有关部门批准的植物生长调节剂。这是为什么?', '[{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685429290947649-植物生长调节剂应用的一些事例.docx\\\",\\\"name\\\":\\\"植物生长调节剂应用的一些事例.docx\\\",\\\"type\\\":1,\\\"filename\\\":\\\"植物生长调节剂应用的一些事例.docx\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685429290947649-植物生长调节剂应用的一些事例.docx\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685429290947649-植物生长调节剂应用的一些事例.docx\\\",\\\"size\\\":12368,\\\"ossFileName\\\":\\\"1685429290947649-植物生长调节剂应用的一些事例.docx\\\",\\\"progress\\\":100}]', NULL, '[{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685427496898833-858b8958591594e7.jpg\\\",\\\"name\\\":\\\"858b8958591594e7.jpg\\\",\\\"type\\\":2,\\\"filename\\\":\\\"858b8958591594e7.jpg\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685427496898833-858b8958591594e7.jpg\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/1685427496898833-858b8958591594e7.jpg\\\",\\\"size\\\":111546,\\\"ossFileName\\\":\\\"1685427496898833-858b8958591594e7.jpg\\\",\\\"width\\\":990,\\\"height\\\":836,\\\"progress\\\":100},{\\\"url\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/168542749689917-u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"name\\\":\\\"u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"type\\\":2,\\\"filename\\\":\\\"u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"fileurl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/168542749689917-u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"fileUrl\\\":\\\"https://xinqiclass.oss-cn-hangzhou.aliyuncs.com/2023-05-30/168542749689917-u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"size\\\":100118,\\\"ossFileName\\\":\\\"168542749689917-u=1011559413,3152698837&fm=253&fmt=auto&app=138&f=JPEG.webp.jpg\\\",\\\"width\\\":450,\\\"height\\\":300,\\\"progress\\\":100}]', 1661184856792928257, 1, 0, 0, NULL, 0, NULL, 0, 1, 0, '2023-05-30 20:12:18', '2023-06-20 00:00:00', 1, NULL, 1661193644765777922, 1, 0, NULL, 1645325062446964737, '2023-05-30 14:18:31', 1645325062446964737, '2023-06-14 22:13:37');";
        // 更新章节
        Set<String> keyList = chapterMap.keySet();
        for (String item : keyList) {
            String value = chapterMap.get(item);
            noticeSql = noticeSql.replaceAll(item, value);
        }
        // 更新班级
        noticeSql = noticeSql.replaceAll("1661184856792928257", classId.toString());
        // 更新发布者信息
        noticeSql = noticeSql.replaceAll("1645325062446964737", userId.toString());
        sql.append(noticeSql);

        // 公告学生日志SQL
        String noticeLogSql = "INSERT INTO discuss.co_course_discuss_student_log(`id`, `class_id`, `content_id`, `student_id`, `is_read`, `is_partake`, `create_by`, `create_time`, `update_by`, `update_time`) " +
            "VALUES (" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1650458704827445249, 1, 1, 1645325062446964737, '2023-05-28 19:39:55', 1650458704827445249, '2023-05-28 20:02:11')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1653986821394567169, 1, 1, 1645325062446964737, '2023-05-28 19:39:55', 1653986821394567169, '2023-05-28 21:06:47')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1654693737893134337, 1, 1, 1645325062446964737, '2023-05-28 19:39:55', 1654693737893134337, '2023-05-28 20:58:16')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1655410589569056769, 0, 0, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 19:39:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1645972241733963777, 0, 0, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 19:39:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1655650372358701057, 0, 0, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 19:39:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1656548738307391489, 1, 1, 1645325062446964737, '2023-05-28 19:39:55', 1656548738307391489, '2023-05-28 19:51:10')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1659403958154625026, 0, 0, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 19:39:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, " + userId + ", 0, 0, 1645325062446964737, '2023-05-28 19:39:55', 1645325062446964737, '2023-05-28 19:39:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1662785767952781314, 1661185385489694721, 1, 1, 1645325062446964737, '2023-05-28 19:39:55', 1661185385489694721, '2023-05-28 20:50:13')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1645972241733963777, 0, 0, 1645325062446964737, '2023-05-30 20:12:18', 1645325062446964737, '2023-05-30 20:12:18')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1650458704827445249, 1, 1, 1645325062446964737, '2023-05-30 20:12:18', 1650458704827445249, '2023-05-30 21:10:41')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1653986821394567169, 1, 1, 1645325062446964737, '2023-05-30 20:12:18', 1653986821394567169, '2023-05-30 21:26:53')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1654693737893134337, 1, 1, 1645325062446964737, '2023-05-30 20:12:18', 1654693737893134337, '2023-05-30 20:58:02')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1655410589569056769, 0, 0, 1645325062446964737, '2023-05-30 20:12:18', 1645325062446964737, '2023-05-30 20:12:18')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1655650372358701057, 0, 0, 1645325062446964737, '2023-05-30 20:12:18', 1645325062446964737, '2023-05-30 20:12:18')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, " + userId + ", 0, 0, 1645325062446964737, '2023-05-30 20:12:18', 1645325062446964737, '2023-05-30 20:12:18')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1656548738307391489, 1, 1, 1645325062446964737, '2023-05-30 20:12:18', 1656548738307391489, '2023-05-31 08:49:44')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1659403958154625026, 1, 0, 1645325062446964737, '2023-05-30 20:12:18', 1659403958154625026, '2023-06-14 21:37:34')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663429659873566721, 1661185385489694721, 1, 1, 1645325062446964737, '2023-05-30 20:12:18', 1661185385489694721, '2023-05-31 08:58:57')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, " + userId + ", 0, 0, 1645325062446964737, '2023-05-30 20:12:23', 1645325062446964737, '2023-05-30 20:12:23')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1645972241733963777, 0, 0, 1645325062446964737, '2023-05-30 20:12:23', 1645325062446964737, '2023-05-30 20:12:23')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1650458704827445249, 1, 1, 1645325062446964737, '2023-05-30 20:12:23', 1650458704827445249, '2023-05-30 21:13:08')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1653986821394567169, 1, 1, 1645325062446964737, '2023-05-30 20:12:23', 1653986821394567169, '2023-05-30 21:24:26')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1654693737893134337, 1, 1, 1645325062446964737, '2023-05-30 20:12:23', 1654693737893134337, '2023-05-30 20:59:22')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1655410589569056769, 0, 0, 1645325062446964737, '2023-05-30 20:12:23', 1645325062446964737, '2023-05-30 20:12:23')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1655650372358701057, 0, 0, 1645325062446964737, '2023-05-30 20:12:23', 1645325062446964737, '2023-05-30 20:12:23')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1656548738307391489, 1, 0, 1645325062446964737, '2023-05-30 20:12:23', 1656548738307391489, '2023-05-31 08:48:49')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1659403958154625026, 1, 0, 1645325062446964737, '2023-05-30 20:12:23', 1659403958154625026, '2023-05-31 09:17:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663424373238587393, 1661185385489694721, 1, 0, 1645325062446964737, '2023-05-30 20:12:23', 1661185385489694721, '2023-05-31 08:57:33');";
        // 更新章节
        Set<String> idsList = idsMap.keySet();
        for (String item : idsList) {
            String value = idsMap.get(item);
            noticeLogSql = noticeLogSql.replaceAll(item, value);
        }
        noticeLogSql = noticeLogSql.replaceAll("1661184856792928257", classId.toString());
        sql.append(noticeLogSql);

        // 执行SQL
        courseMapper.insertAllSql(sql.toString());
    }

}
